package com.bh.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.core.domain.entity.SysUser;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.order.domain.JcOrderTrack;
import com.bh.order.mapper.JcOrderTrackMapper;
import com.bh.order.service.IJcOrderTrackService;
import com.bh.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 订单追踪Service业务层处理
 *
 * @author slx
 * @date 2023-04-22
 */

@Service
public class JcOrderTrackServiceImpl extends ServiceImpl<JcOrderTrackMapper, JcOrderTrack> implements IJcOrderTrackService {
    @Autowired
    private JcOrderTrackMapper jcOrderTrackMapper;
    @Autowired
    private SysUserMapper userMapper;
    /**
     * 查询订单追踪
     *
     * @param id 订单追踪主键
     * @return 订单追踪
     */
    @Override
    public JcOrderTrack selectJcOrderTrackById(Long id) {
        return jcOrderTrackMapper.selectById(id);
    }

    /**
     * 查询订单追踪列表
     *
     * @param jcOrderTrack 订单追踪
     * @return 订单追踪
     */
    @Override
    public List<JcOrderTrack> selectJcOrderTrackList(JcOrderTrack jcOrderTrack) {
        List<JcOrderTrack> jcOrderTracks = jcOrderTrackMapper.selectJcOrderTrackList(jcOrderTrack);
        jcOrderTracks.forEach(track -> {
            SysUser user = userMapper.selectUserByUserName(track.getCreateName());
            track.setCreateName(user.getUserName());
        });
        return jcOrderTracks;
    }

    @Override
    public List<JcOrderTrack> selectJcOrderTrackListByOrderId(Long orderId) {
        JcOrderTrack jcOrderTrack = new JcOrderTrack();
        jcOrderTrack.setJcOrderId(orderId);
        List<JcOrderTrack> jcOrderTracks = jcOrderTrackMapper.selectJcOrderTrackList(jcOrderTrack);
        jcOrderTracks.forEach(track -> {
            SysUser user = userMapper.selectUserByUserName(track.getCreateName());
            if (user != null) {
                track.setCreateName(user.getUserName());
            }
        });
        return jcOrderTracks;
    }

    /**
     * 新增订单追踪
     *
     * @param jcOrderTrack 订单追踪
     * @return 结果
     */
    @Override
    public int insertJcOrderTrack(JcOrderTrack jcOrderTrack) {
        jcOrderTrack.setCreateName(SecurityUtils.getUsername());
        jcOrderTrack.setCreateTime(DateUtils.getNowDate());
        return jcOrderTrackMapper.insert(jcOrderTrack);
    }

    /**
     * 批量删除订单追踪
     *
     * @param ids 需要删除的订单追踪主键
     * @return 结果
     */
    @Override
    public int deleteJcOrderTrackByIds(Long[] ids) {
        return jcOrderTrackMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除订单追踪信息
     *
     * @param id 订单追踪主键
     * @return 结果
     */
    @Override
    public int deleteJcOrderTrackById(Long id) {
        return jcOrderTrackMapper.deleteById(id);
    }


    @Override
    public int addTrack(Long orderId, String ... eventAndImgs) {
        //在途跟踪登记
        JcOrderTrack orderTrack = new JcOrderTrack();
        orderTrack.setTime(DateUtils.getNowDate());
        orderTrack.setCreateTime(new Date());
        orderTrack.setCreateName(SecurityUtils.getUsername());
        orderTrack.setEvent(eventAndImgs[0]);
        if (eventAndImgs.length > 1) {
            orderTrack.setImgs(eventAndImgs[1]);
        }
        orderTrack.setJcOrderId(orderId);
        orderTrack.setStatus(1);
        return jcOrderTrackMapper.insert(orderTrack);
    }
}
